$(document).ready(function () {
	// Editor
	var editor = new wangEditor('newsContent');
	
	editor.config.uploadImgUrl = '/admin/uploadImg';
	editor.config.hideLinkImg = true;
	
    editor.create();
    
    // Publish
    $('form').submit(function (event) {
    	event.preventDefault();
    	var fd = new FormData();
	    fd.append("title", $('#title').val());
	    fd.append("smallTitle", $('#smallTitle').val());
	    fd.append("summary", $('#summary').val());
	    fd.append("content", editor.$txt.html());
	    fd.append("smallPicture", $('#smallPicture')[0].files[0]);
	    fd.append("classify", $('#send').attr('data-value'));
	    fd.append("oldSmallPicture", $('#smallPicture').attr('data-oldSmallPicture'));
	    fd.append("newsId", $('#send').attr('data-newsId'));
	    // 发布新闻
	    if(!$('#send').attr('data-newsId')) {
		    $.ajax({
		        type:'POST',
		        dataType:'json',
		        processData: false,  // 告诉JSLite不要去处理发送的数据
		        contentType: false,   // 告诉JSLite不要去设置Content-Type请求头
		        data:fd,
		        url:'/admin/published',
			    success: function (data) {
			    	if(data.status) {
			    		swal({
						  title: "发布成功",
						  type: "success",
						  closeOnConfirm: false
						},
						function(){
						  window.location.href="/admin";
						});
			    	}
			    }
		    });
		// 更新新闻
		}else {
			$.ajax({
		        type:'POST',
		        dataType:'json',
		        processData: false,  // 告诉JSLite不要去处理发送的数据
		        contentType: false,   // 告诉JSLite不要去设置Content-Type请求头
		        data:fd,
		        url:'/admin/publishedUpdate',
			    success: function (data) {
			    	if(data.status) {
			    		swal({
						  title: "修改成功",
						  type: "success",
						  closeOnConfirm: false
						},
						function(){
						  window.location.href="/admin";
						});
			    	}
			    }
		    });
		}
    });
});
